package it.eng.area118.mdocommon.android.network;

import it.eng.area118.mdocommon.android.log.BasicLogger;
import it.eng.area118.mdocommon.android.network.NetworkData.ConnectionState;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;

public class NetworkUtils {

	public static NetworkData<?> createNetworkData(
			ConnectivityManager connectivityManager) {
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		NetworkData<NetworkInfo> newNetworkData = new NetworkData<NetworkInfo>();
		newNetworkData.setExtraData(activeNetInfo);
		if (activeNetInfo != null) {
			DetailedState detailedState = activeNetInfo.getDetailedState();
			BasicLogger.writeLog("NetworkUtils", "DETAILED STATE: "
					+ detailedState);
			newNetworkData.setNetworkType(activeNetInfo.getTypeName());
			if (detailedState != null) {
				if (detailedState.equals(DetailedState.CONNECTED)) {
					newNetworkData
							.setConnectionState(ConnectionState.CONNECTED);
					BasicLogger.writeLog("NetworkUtils",
							"DETAILED STATE CONNECTED: " + detailedState);
				} else if (detailedState.equals(DetailedState.CONNECTING)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.DISCONNECTED)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.DISCONNECTING)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.SUSPENDED)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);
				} else if (detailedState.equals(DetailedState.AUTHENTICATING)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.FAILED)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.IDLE)) {
					newNetworkData
							.setConnectionState(ConnectionState.CONNECTED);

				} else if (detailedState.equals(DetailedState.OBTAINING_IPADDR)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.SCANNING)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				} else if (detailedState.equals(DetailedState.SUSPENDED)) {
					newNetworkData
							.setConnectionState(ConnectionState.DISCONNECTED);

				}

			} else {
				// TODO: ??
				BasicLogger.writeLog("NetworkUtils", "DETAILED STATE is null ");
			}
		} else {
			newNetworkData.setNetworkType("OFFLINE");
			newNetworkData.setConnectionState(ConnectionState.DISCONNECTED);
		}
		return newNetworkData;
	}
}
